from keras.datasets import mnist
from keras import models
from keras import layers
from keras.utils import to_categorical
import matplotlib.pyplot as plt

(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

# print(train_images.shape)  # (60000, 28, 28)
# print(train_labels.shape)  # (60000,)
# print(test_images.shape)  # (10000, 28, 28)
# print(test_labels.shape)  # (10000,)
# print(train_labels[0:10])  # 去标签的前10个，看看其中的数据长什么样子，原来都是代表的是哪个数。[5 0 4 1 9 2 1 3 1 4]
# print(train_images[0])  # 去一个样例，可以看出其中的数据范围是0~255


network = models.Sequential()
network.add(layers.Dense(512, activation='relu', input_shape=(28 * 28,)))
network.add(layers.Dense(10, activation='softmax'))
# network.summary()
network.compile(optimizer='rmsprop',
                loss='categorical_crossentropy',
                metrics=['accuracy'])
train_images = train_images.reshape((60000, 28 * 28))
train_images = train_images.astype('float32') / 255
test_images = test_images.reshape((10000, 28 * 28))
test_images = test_images.astype('float32') / 255
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
network.fit(train_images, train_labels, epochs=5, batch_size=128)
test_loss, test_acc = network.evaluate(test_images, test_labels)
print("-" * 50)
print('test_loss:', test_loss)
print('test_acc:', test_acc)
